package values

import (
	"encoding/json"
	"errors"
	"fmt"
	"strings"
	"time"
)

// ToSliceMap 转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回空的 []map[K]V 切片
//   - 通过断言快速处理基本切片 []map[K]V
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration | time.Month | time.Weekday
func ToSliceMap[K TypeBase, V TypeBase](v any) []map[K]V {
	val, _ := ToSliceMapE[K, V](v)
	return val
}

// ToSliceMapD 转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回defaultVal指定的默认值
//   - 通过断言快速处理基本切片 []map[K]V
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration | time.Month | time.Weekday
func ToSliceMapD[K TypeBase, V TypeBase](v any, defaultVal []map[K]V) []map[K]V {
	if v == nil {
		return defaultVal
	}
	if val, err := ToSliceMapE[K, V](v); err == nil {
		return val
	}
	return defaultVal
}

// ToSliceMapE 转换目标为任意类型的 []map[K]V 切片, 如果转换失败, 则返回空的 []map[K]V 切片和错误信息
//   - 通过断言快速处理基本切片 []map[K]V
//   - TypeBase: string | bool |	int | int8 | int16 | int32 | int64 |	uint | uint8 | uint16 | uint32 | uint64 |	float32 | float64 |	time.Time | time.Duration | time.Month | time.Weekday
func ToSliceMapE[K TypeBase, V TypeBase | any](v any) ([]map[K]V, error) {
	toVal := []map[K]V{}
	if v == nil {
		return toVal, errors.New("ToSliceMapE: nil pointer not support")
	}
	// 先通过断言判断待转换值是否为 []map[K]V / []map[K]any / JsonString 类型
	switch vs := v.(type) {
	case []map[K]V:
		return _toSliceMapAppend(vs)
	case *[]map[K]V:
		return _toSliceMapAppend(*vs)
	case []map[K]any:
		return _toSliceMapAnyV[K, V](vs)
	case *[]map[K]any:
		return _toSliceMapAnyV[K, V](*vs)
	case []map[any]any:
		return _toSliceMapAnyKV[K, V](vs)
	case *[]map[any]any:
		return _toSliceMapAnyKV[K, V](*vs)
	case string:
		if vs != "" && strings.HasPrefix(vs, "[") && strings.HasSuffix(vs, "]") && json.Valid([]byte(vs)) {
			var toAny []map[K]any
			if err := json.Unmarshal([]byte(vs), &toAny); err == nil {
				return _toSliceMapAnyV[K, V](toAny)
			} else {
				return toVal, fmt.Errorf("ToSliceMapE: string type to []map[K]V type, failed to unmarshal JSON: %s", err.Error())
			}
		}
		return toVal, errors.New("ToSliceMapE: string type to []map[K]V  type not support")
	case *string:
		if *vs != "" && strings.HasPrefix(*vs, "[") && strings.HasSuffix(*vs, "]") && json.Valid([]byte(*vs)) {
			var toAny []map[K]any
			if err := json.Unmarshal([]byte(*vs), &toAny); err == nil {
				return _toSliceMapAnyV[K, V](toAny)
			} else {
				return toVal, fmt.Errorf("ToSliceMapE: *string type to []map[K]V  type, failed to unmarshal JSON: %s", err.Error())
			}
		}
		return toVal, errors.New("ToSliceMapE: *string type to []map[K]V  type  not support")
	}
	// 如果待转换值是Golang 常用的基本类型或指针, 则直接返回错误信息
	// 注意：_isTypeBasic 只能放到 _toSliceAsBaseV 后面, _toSliceAsBaseV 检查json string,
	if _isTypeBasic(v) {
		return []map[K]V{}, errors.New("ToSliceMapE: basic type to type []map[K]V not support")
	}
	// 通过断言判断待转换值是否为 []map[X]X 类型
	if toVal, err, next := _toSliceMapWithKey[K, V, string](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, int](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, uint](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, int64](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, uint64](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, int32](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, uint32](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, int16](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, uint16](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, int8](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, uint8](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, float32](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, float64](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, float64](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, time.Time](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, time.Duration](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, time.Month](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, time.Weekday](v); !next {
		return toVal, err
	}
	if toVal, err, next := _toSliceMapWithKey[K, V, bool](v); !next {
		return toVal, err
	}
	return toVal, fmt.Errorf("ToSliceMapE: type %T to type %T not support", v, toVal)
}
func _toSliceMapAppend[K TypeBase, V TypeBase | any](vs []map[K]V) ([]map[K]V, error) {
	if vs == nil {
		return []map[K]V{}, errors.New("ToSliceMapE: nil pointer not support")
	}
	if len(vs) == 0 {
		return []map[K]V{}, nil
	}
	toVal := make([]map[K]V, len(vs))
	copy(toVal, vs)
	return toVal, nil
}
func _toSliceMapAnyV[K TypeBase, V TypeBase | any](vs []map[K]any) ([]map[K]V, error) {
	if vs == nil {
		return []map[K]V{}, errors.New("ToSliceMapE: nil pointer not support")
	}
	if len(vs) == 0 {
		return []map[K]V{}, nil
	}
	toMap := make([]map[K]V, len(vs))

	for key, item := range vs {
		if item == nil {
			return []map[K]V{}, errors.New("ToSliceMapE: nil pointer not support")
		}
		if len(item) == 0 {
			toMap[key] = map[K]V{}
			continue
		}
		toVal := make(map[K]V, len(item))
		for k, v := range item {
			if v == nil {
				return []map[K]V{}, errors.New("ToSliceMapE: nil pointer not support")
			}
			if val, ok := v.(V); ok {
				toVal[k] = val
				continue
			} else if val, err := _anyToBase[V](v); err == nil {
				toVal[k] = val
			} else {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: %T to type %T,error: %s", v, val, err.Error())
			}
		}
		toMap[key] = toVal
	}
	return toMap, nil
}
func _toSliceMapAnyKV[K TypeBase, V TypeBase | any](vs []map[any]any) ([]map[K]V, error) {
	if vs == nil {
		return []map[K]V{}, errors.New("ToSliceMapE: nil pointer not support")
	}
	if len(vs) == 0 {
		return []map[K]V{}, nil
	}
	toVal := make([]map[K]V, len(vs))
	var (
		toK K
		toV V
	)
	for i, maps := range vs {
		if maps == nil {
			return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps is nil, nil pointer not support", i)
		}
		if len(maps) == 0 {
			toVal[i] = map[K]V{}
			continue
		}
		toMap := make(map[K]V, len(maps))
		for key, item := range maps {
			if key == nil || item == nil {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps key or val is nil, nil pointer not support", i)
			}
			if val, ok := key.(K); ok {
				toK = val
			} else if val, err := _anyToBase[K](key); err == nil {
				toK = val
			} else {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps key[%#v] to type of %T, error:%s", i, key, toK, err.Error())
			}
			if val, ok := item.(V); ok {
				toV = val
			} else if val, err := _anyToBase[V](item); err == nil {
				toV = val
			} else {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps val[%#v] to type of %T,, error:%s", i, item, toV, err.Error())
			}
			toMap[toK] = toV
		}
		toVal[i] = toMap
	}
	return toVal, nil
}

func _toSliceMapWithKey[K TypeBase, V TypeBase | any, Key TypeBase](v any) ([]map[K]V, error, bool) {
	switch vs := v.(type) {
	default:
		return []map[K]V{}, nil, true // Next "true" To continue
	case []map[Key]V:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]any:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]string:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]bool:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]int:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]int8:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]int16:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]int32:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]int64:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]uint:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]uint8:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]uint16:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]uint32:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]uint64:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]float32:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]float64:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]time.Time:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]time.Duration:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]time.Weekday:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]time.Month:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]json.Number:
		return _toSliceMapWithLoop[K, V](vs)
	case []map[Key]error:
		return _toSliceMapWithLoop[K, V](vs)
	case *[]map[Key]any:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]string:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]bool:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]int:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]int8:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]int16:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]int32:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]int64:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]uint:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]uint8:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]uint16:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]uint32:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]uint64:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]float32:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]float64:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]time.Time:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]time.Duration:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]time.Weekday:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]time.Month:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]json.Number:
		return _toSliceMapWithLoop[K, V](*vs)
	case *[]map[Key]error:
		return _toSliceMapWithLoop[K, V](*vs)
	case []*map[Key]any:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]string:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]bool:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]int:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]int8:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]int16:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]int32:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]int64:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]uint:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]uint8:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]uint16:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]uint32:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]uint64:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]float32:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]float64:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]time.Time:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]time.Duration:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]time.Weekday:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]time.Month:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]json.Number:
		return _toSliceMapWithLoopPtr[K, V](vs)
	case []*map[Key]error:
		return _toSliceMapWithLoopPtr[K, V](vs)
	}
}
func _toSliceMapWithLoop[K TypeBase, V TypeBase | any, vsK TypeBase, vsV any](vs []map[vsK]vsV) ([]map[K]V, error, bool) {
	if vs == nil {
		return []map[K]V{}, errors.New("ToMapSliceE: nil pointer not support"), false // Next "false" To Break
	}
	if len(vs) == 0 {
		return []map[K]V{}, nil, false // Next "false" To Break
	}
	toVal := make([]map[K]V, len(vs))
	var (
		toK K
		toV V
	)
	for i, maps := range vs {
		if maps == nil {
			return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps is nil, nil pointer not support", i), false
		}
		if len(maps) == 0 {
			toVal[i] = map[K]V{}
			continue
		}
		toMap := make(map[K]V, len(maps))
		for key, item := range maps {
			if any(item) == nil {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps key or val is nil, nil pointer not support", i), false
			}
			if val, ok := any(key).(K); ok {
				toK = val
			} else if val, err := _anyToBase[K](key); err == nil {
				toK = val
			} else {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps key[%#v] to type of %T, error:%s", i, key, toK, err.Error()), false
			}
			if val, ok := any(item).(V); ok {
				toV = val
			} else if val, err := _anyToBase[V](item); err == nil {
				toV = val
			} else {
				return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps val[%#v] to type of %T,, error:%s", i, item, toV, err.Error()), false
			}
			toMap[toK] = toV
		}
		toVal[i] = toMap
	}
	return toVal, nil, false
}
func _toSliceMapWithLoopPtr[K TypeBase, V TypeBase | any, vsK TypeBase, vsV any](vs []*map[vsK]vsV) ([]map[K]V, error, bool) {
	if vs == nil {
		return []map[K]V{}, errors.New("ToMapSliceE: nil pointer not support"), false // Next "false" To Break
	}
	if len(vs) == 0 {
		return []map[K]V{}, nil, false // Next "false" To Break
	}
	toMaps := make([]map[vsK]vsV, len(vs))

	for i, maps := range vs {
		if maps == nil {
			return []map[K]V{}, fmt.Errorf("ToSliceMapE: slice index[%d] maps is nil, nil pointer not support", i), false
		}
		toMaps[i] = *maps
	}
	return _toSliceMapWithLoop[K, V](toMaps)
}
